/////////////////////////////////////////////////////////////////////////////////////////
//MainDlg.cpp
//Author: EmbedVision
/////////////////////////////////////////////////////////////////////////////////////////
#include "MainDlg.h"
#include <QDebug>
CMainDlg* CMainDlg::m_this_module = NULL;

CMainDlg::CMainDlg(QWidget* parent) : QDialog(parent)
{
	//设置对话框的标题、位置与大小
	this->setWindowTitle(QString::fromUtf8("串口通信测试程序"));
	this->setGeometry(0, 40, 800, 440);

	//创建按钮对象
        m_openBtn = new QPushButton(QString::fromUtf8("open uart"), this);
        m_stopBtn = new QPushButton(QString::fromUtf8("close uart"), this);
        m_sendBtn = new QPushButton(QString::fromUtf8("send test"), this);
        m_cancelBtn = new QPushButton(QString::fromUtf8("exit app"), this);
	m_portUsbBtn = new QPushButton(QString::fromUtf8("USB"), this);
	m_portCom1Btn = new QPushButton(QString::fromUtf8("COM2"), this);
	m_portCom2Btn = new QPushButton(QString::fromUtf8("COM3"), this);
	m_portCom3Btn = new QPushButton(QString::fromUtf8("COM4"), this);
	
	//设置按钮对象的位置
	m_openBtn->setGeometry(580, 40, 210, 60);
	m_stopBtn->setGeometry(580, 140, 210, 60);
	m_sendBtn->setGeometry(580, 240, 210, 60);
	m_cancelBtn->setGeometry(580, 340, 210, 60);
	m_portUsbBtn->setGeometry(100, 340, 60, 60);
	m_portCom1Btn->setGeometry(200, 340, 60, 60);
	m_portCom2Btn->setGeometry(300, 340, 60, 60);
	m_portCom3Btn->setGeometry(400, 340, 60, 60);
	
	//设置按钮对象的样式及状态
	m_openBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
	m_stopBtn->setEnabled(false);
	m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
	m_sendBtn->setEnabled(false);
	m_sendBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
	m_cancelBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
	m_com_index = 0;
	updateComBtnStatus(1, m_com_index);
	
	//创建列表框对象
	m_listWidget = new QListWidget(this);
        m_listWidget->setGeometry(20, 20, 540, 300);
      //  m_listWidget->setGeometry(20, 20, 1024, 768);
	m_listWidget->clear();

	//关联信号与槽
	connect(m_openBtn, SIGNAL(clicked()), this, SLOT(openBtn()));
	connect(m_stopBtn, SIGNAL(clicked()), this, SLOT(stopBtn()));
	connect(m_sendBtn, SIGNAL(clicked()), this, SLOT(sendBtn()));
	connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
	connect(m_portUsbBtn, SIGNAL(clicked()), this, SLOT(portUsbBtn()));
	connect(m_portCom1Btn, SIGNAL(clicked()), this, SLOT(portCom1Btn()));
	connect(m_portCom2Btn, SIGNAL(clicked()), this, SLOT(portCom2Btn()));
	connect(m_portCom3Btn, SIGNAL(clicked()), this, SLOT(portCom3Btn()));
	
	m_send_num = 0;
	m_this_module = this;
}

CMainDlg::~CMainDlg()
{

}

void CMainDlg::updateComBtnStatus(int enable_flag, int current_id)
{
	if(0 == current_id)
	{
		m_portUsbBtn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(0,255,0); border-radius:30px}");
		m_portCom1Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom2Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom3Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
	}
	if(1 == current_id)
	{
		m_portUsbBtn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom1Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(0,255,0); border-radius:30px}");
		m_portCom2Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom3Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
	}
	if(2 == current_id)
	{
		m_portUsbBtn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom1Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom2Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(0,255,0); border-radius:30px}");
		m_portCom3Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
	}
	if(3 == current_id)
	{
		m_portUsbBtn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom1Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom2Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(255,0,0); border-radius:30px}");
		m_portCom3Btn->setStyleSheet("QPushButton{font-size:20px; color:rgb(255,255,255); background-color:rgb(0,255,0); border-radius:30px}");
	}
	
	if(1 == enable_flag)
	{
		m_portUsbBtn->setEnabled(true);
		m_portCom1Btn->setEnabled(true);
		m_portCom2Btn->setEnabled(true);
		m_portCom3Btn->setEnabled(true);
	}
	else
	{
		m_portUsbBtn->setEnabled(false);
		m_portCom1Btn->setEnabled(false);
		m_portCom2Btn->setEnabled(false);
		m_portCom3Btn->setEnabled(false);
	}
}

void CMainDlg::portUsbBtn()
{
	m_com_index = 0;
	updateComBtnStatus(1, m_com_index);
}

void CMainDlg::portCom1Btn()
{
	m_com_index = 1;
	updateComBtnStatus(1, m_com_index);
}

void CMainDlg::portCom2Btn()
{
	m_com_index = 2;
	updateComBtnStatus(1, m_com_index);
}

void CMainDlg::portCom3Btn()
{
	m_com_index = 3;
	updateComBtnStatus(1, m_com_index);
}

void CMainDlg::moveEvent(QMoveEvent* event)
{
	//保持窗口在触摸屏中的位置不变
        this->setGeometry(0, 40, 1024, 768);

	QDialog::moveEvent(event);
}

//打开串口对象，并开启监听线程
void CMainDlg::openBtn()
{
	//打开并初始化串口设备
	char dev_name[100];
	if(0 == m_com_index)
		sprintf(dev_name, "/dev/ttyUSB0");
	if(1 == m_com_index)
                sprintf(dev_name, "/dev/ttyO1");
	if(2 == m_com_index)
                sprintf(dev_name, "/dev/ttyO2");
	if(3 == m_com_index)
                sprintf(dev_name, "/dev/ttyO3");
	if(-1 == m_com.InitPort(dev_name, B115200))
	{
		QMessageBox::information(this, QString::fromUtf8("错误"), QString::fromUtf8("打开串口失败！"));;
		return;
	}
	
	//开始监听
	m_com.StartListening(ComRecvProc);
	
	//改变按钮状态
	m_openBtn->setEnabled(false);
	m_openBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
	m_stopBtn->setEnabled(true);
	m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
	m_sendBtn->setEnabled(true);
	m_sendBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
	m_cancelBtn->setEnabled(false);
	m_cancelBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
	updateComBtnStatus(0, m_com_index);
}

//停止监听线程，关闭串口对象
void CMainDlg::stopBtn()
{
	//停止监听
	m_com.StopListening();
	
	//关闭串口设备
	m_com.ClosePort();
	
	//改变按钮状态
	m_openBtn->setEnabled(true);
	m_openBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
	m_stopBtn->setEnabled(false);
	m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
	m_sendBtn->setEnabled(false);
	m_sendBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
	m_cancelBtn->setEnabled(true);
	m_cancelBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
	updateComBtnStatus(1, m_com_index);
}

//串口发送测试
void CMainDlg::sendBtn()
{
#if 0
	char send_buf[1024];
	sprintf(send_buf, "from linux: com test:%d", m_send_num++);
	
	m_com.SendData(send_buf, strlen(send_buf)+1);
#endif

        unsigned char send_buf[1024];
        send_buf[0] = m_send_num % 256;
        m_com.SendData((char*)send_buf, 1);
        m_send_num++;

}


void CMainDlg::ComRecvProc(const char *data, const int data_len)
{
         m_this_module->ComRecv(data, data_len);
}

#if 0
//对接收到的串口消息进行处理
void CMainDlg::ComRecv(const char *data, const int data_len)
{
       /*  m_listWidget->addItem(QString::fromUtf8(data));
          int nCount = m_listWidget->count();
          m_listWidget->setCurrentRow(nCount-1);*/
        unsigned char cmd_rev=(unsigned char)(data[0]);
        switch(cmd_rev)
        {
          case 0x84:
               // startBtn();
                qDebug() << "receiver len = " << data_len << "nei rong:" << data[0];
                break;
          case 0x85:
                //stopBtn();
                qDebug() << "receiver len = " << data_len << "nei rong:" << data[1];
           default:
                break;
        }
}
#endif

#if 0
//对接收到的串口消息进行处理
void CMainDlg::ComRecv(const char *data, const int data_len)
{
        QString msg;
        for (int i = 0; i < data_len; i++)
        {
                qint8 outChar = data[i];
                QString str = QString("%1").arg(outChar&0xFF, 2, 16, QLatin1Char('0'));
                msg += str;
        }

        m_listWidget->addItem(msg);
        int nCount = m_listWidget->count();
        m_listWidget->setCurrentRow(nCount-1);
}
#endif

void CMainDlg::ComRecv(const char *data, const int data_len)
{
        QString msg;
        for (int i = 0; i < data_len; i++)
        {
                qint8 outChar = data[i];
                QString str = QString("%1").arg(outChar&0xFF, 2, 16, QLatin1Char('0'));
                str += " ";
                msg += str;
        }

        m_listWidget->addItem(msg);
        int nCount = m_listWidget->count();
        m_listWidget->setCurrentRow(nCount-1);
}
